﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MasMembers.DAL;
using MasMembers.UTIL;
namespace MasMembers
{
    public partial class SelectMemberDialoge : Form
    {
        public MainForm mainForm;
        public int caller = 0;
        //Caller Values
        //0: Memeber basic info
        //1: Partner Info
        //2: Family Info

        public SelectMemberDialoge()
        {
            InitializeComponent();
        }

        public SelectMemberDialoge(int caller)
            : this()
        {
            this.caller = caller;
        }

        private void SelectMemberDialoge_Load(object sender, EventArgs e)
        {
            gridMembers.DataSource = MemberBasicInfo.GetAllMembers();
        }

        private void txtMemName_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                gridMembers.DataSource = MemberBasicInfo.GetMembersByName("%" + txtMemName.Text +"%");
        }

        private void gridMembers_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex != -1 && e.ColumnIndex != -1)
            {
                if (caller == 0) //Memeber
                {
                    mainForm.publishAllPanelsData(((List<MemberBasicInfoEntity>)gridMembers.DataSource)[e.RowIndex]);                
                }
                else if (caller == 1) //Partner
                {
                    mainForm.lblExistingPartner.Text = ((List<MemberBasicInfoEntity>)gridMembers.DataSource)[e.RowIndex].m_Id.ToString();
                    mainForm.publishPartnerDataToForm(DALUtil.mapMemToPart(((List<MemberBasicInfoEntity>)gridMembers.DataSource)[e.RowIndex]));
                }
                else if (caller == 2)// Family
                {
                    mainForm.lblExitingMember.Text = ((List<MemberBasicInfoEntity>)gridMembers.DataSource)[e.RowIndex].m_Id.ToString();
                    mainForm.publishFamilyDataToForm(DALUtil.mapMemToFam(((List<MemberBasicInfoEntity>)gridMembers.DataSource)[e.RowIndex]));
                }
                this.Close();
            }
        }
    }
}
